
	  
using Youz.Common.BLL;
using Youz.Common.Helper;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace Youz.CreditRepay.Manage.Handlers
{
    public partial class Log_OperationLogHandler : IHttpHandler, IRequiresSessionState
    {
        Log_OperationLogBLL bll = new Log_OperationLogBLL();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string _type = WebHelper.Get("type");
            string result = string.Empty;
            switch (_type)
            {
                case "get":
                    result = Get();
                    break;
                case "list":
                    result = List();
                    break;
                case "save":
                    result = Save();
                    break;
                case "delete":
                    result = Delete();
                    break;
            }

            context.Response.Write(result);
            context.Response.End();
        }

        private string Get()
        {
            Guid OperationID = WebHelper.Post<Guid>("OperationID");

            return bll.GetJson(OperationID);
        }

        private string List()
        {
            int pageIndex = WebHelper.Post<int>("start");
            int pageSize = WebHelper.Post<int>("length");

            string MemberAccount = WebHelper.Post("MemberAccount").Trim();
            string LoginType = WebHelper.Post("LoginType").Trim();
            string StartDate = WebHelper.Post("StartDate");
            string EndDate = WebHelper.Post("EndDate");
            string MemberAccountSub = WebHelper.Post("MemberAccountSub").Trim();
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" 1=1 ");
            if (!string.IsNullOrEmpty(MemberAccountSub))
            {
                sb.AppendFormat(" and MemberAccount = '{0}'", MemberAccountSub);
            }
            if (!string.IsNullOrEmpty(MemberAccount))
            {
                sb.AppendFormat(" and MemberAccount like '%{0}%'", MemberAccount);
            }
            if (!string.IsNullOrEmpty(LoginType))
            {
                sb.AppendFormat(" and LoginType = '{0}'", LoginType);
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                sb.AppendFormat(" and CreateTime >= '{0}'", StartDate);
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                EndDate = Convert.ToDateTime(EndDate).AddDays(1).ToString("yyyy-MM-dd");
                sb.AppendFormat(" and CreateTime < '{0}'", EndDate);
            }

            int total = 0;
            DataTable dt = bll.Gets(sb.ToString(), pageIndex, pageSize, out total);

            return Utility.GetJsonByDataTableHaveTime(total, dt);
        }

        private string Save()
        {
            string id = WebHelper.Post("OperationID");
            bool isAdd = string.IsNullOrWhiteSpace(id);
            Log_OperationLog model = new Log_OperationLog();

            if (isAdd)
            {
            }
            else
            {
                model = bll.Get(WebHelper.Post<Guid>("OperationID"));
            }

            model.MemberAccount = WebHelper.Post<String>("MemberAccount");
            model.ModuleName = WebHelper.Post<String>("ModuleName");
            model.FunctionName = WebHelper.Post<String>("FunctionName");
            model.URI = WebHelper.Post<String>("URI");
            model.PostData = WebHelper.Post<String>("PostData");
            model.OperationContent = WebHelper.Post<String>("OperationContent");
            model.CreateTime = WebHelper.Post<DateTime>("CreateTime");
            model.LoginID = WebHelper.Post<Guid>("LoginID");
            model.LoginType = WebHelper.Post<Int32>("LoginType");


            if (isAdd ? bll.Add(model) : bll.Update(model))
            {
                return Utility.AjaxResult(true, AjaxResultType.Save);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Save);
            }
        }

        private string Delete()
        {
            if (bll.Delete(WebHelper.Post<Guid>("OperationID")))
            {
                return Utility.AjaxResult(true, AjaxResultType.Delete);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Delete);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

  